package com.example.dyxeapplication.menu.bermain;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class ListItemHandler {

	private static final String ns = null;

	public ArrayList<Item> parse(InputStream in) throws XmlPullParserException,
			IOException {
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(in, null);
			parser.nextTag();
			return readFeed(parser);
		} finally {
			in.close();
		}
	}

	private ArrayList<Item> readFeed(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		ArrayList<Item> items = new ArrayList<Item>();

		parser.require(XmlPullParser.START_TAG, ns, "listitem");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the entry tag
			if (name.equals("item")) {
				items.add(readEntry(parser));
			} else {
				skip(parser);
			}
		}
		return items;
	}

	private Item readEntry(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_ITEM);
		String id = null;
		String head = null;
		String text = null;
		String image = null;
		String sound = null;
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			if (name.equals(Item.KEY_ID)) {
				id = readID(parser);
			} else if (name.equals(Item.KEY_HEAD)) {
				head = readHead(parser);
			} else if (name.equals(Item.KEY_TEXT)) {
				text = readText(parser);
			} else if (name.equals(Item.KEY_IMAGE)) {
				image = readImage(parser);
			} else if (name.equals(Item.KEY_SOUND)) {
				sound = readSound(parser);
			} else {
				skip(parser);
			}
		}
		if ("".equals(id)) {
			return new Item(Integer.parseInt(head), text, image, sound);
		} else {
			return new Item(Integer.parseInt(id), Integer.parseInt(head), text,
					image, sound);
		}

	}

	private String readID(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_ID);
		String id = readXML(parser);
		parser.require(XmlPullParser.END_TAG, ns, Item.KEY_ID);
		return id;
	}

	private String readHead(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_HEAD);
		String head = readXML(parser);
		parser.require(XmlPullParser.END_TAG, ns, Item.KEY_HEAD);
		return head;
	}

	private String readText(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_TEXT);
		String text = readXML(parser);
		parser.require(XmlPullParser.END_TAG, ns, Item.KEY_TEXT);
		return text;
	}

	private String readImage(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_IMAGE);
		String image = readXML(parser);
		parser.require(XmlPullParser.END_TAG, ns, Item.KEY_IMAGE);
		return image;
	}

	private String readSound(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, ns, Item.KEY_SOUND);
		String sound = readXML(parser);
		parser.require(XmlPullParser.END_TAG, ns, Item.KEY_SOUND);
		return sound;
	}

	private String readXML(XmlPullParser parser) throws IOException,
			XmlPullParserException {
		String result = "";
		if (parser.next() == XmlPullParser.TEXT) {
			result = parser.getText();
			parser.nextTag();
		}
		return result;
	}

	private void skip(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		if (parser.getEventType() != XmlPullParser.START_TAG) {
			throw new IllegalStateException();
		}
		int depth = 1;
		while (depth != 0) {
			switch (parser.next()) {
			case XmlPullParser.END_TAG:
				depth--;
				break;
			case XmlPullParser.START_TAG:
				depth++;
				break;
			}
		}
	}
}
